package net.mingsoft.document.biz.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.io.Files;
import net.mingsoft.basic.core.SessionContext;
import net.mingsoft.basic.util.DateUtil;
import net.mingsoft.dashboard.action.q.sharing.SharingCancelQuery;
import net.mingsoft.dashboard.action.q.sharing.SharingCheckPwdQuery;
import net.mingsoft.dashboard.action.q.sharing.SharingListQuery;
import net.mingsoft.dashboard.action.r.FileShareResponse;
import net.mingsoft.document.biz.FileShareBiz;
import net.mingsoft.document.dao.FileShareDao;
import net.mingsoft.document.entity.FileShareEntity;
import net.mingsoft.document.util.FileTypeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author gengfa
 * @date 22/02/09
 */
@Service
public class FileShareBizImpl implements FileShareBiz {
    @Resource
    FileShareDao fileShareDao;

    private static final String ENABLED = "ENABLED";
    private static final String DISABLED = "DISABLED";
    private static final String DELETED = "DELETED";

    @Override
    public Page<FileShareResponse> myShareList(SharingListQuery query) {
        Page<FileShareResponse> page = fileShareDao.selectFileShareListByUserId(
                new Page<>(query.getPageNo(), query.getPageSize()),
                SessionContext.session().getIntegerId(), query.getCreatedDate());
        page.getRecords().forEach(it -> {
            @SuppressWarnings(value = "all")
            final String fileExt = Files.getFileExtension(it.getFileName());
            it.setStatus(getStatus(it.getShareExpired())).setFileExt(fileExt).setFileType(FileTypeUtil.getType(fileExt));
            if (StringUtils.equals(it.getShareStatus(), DELETED)) {
                it.setStatus(DISABLED);
            }
        });
        return page;
    }

    @Override
    public Page<FileShareResponse> receivedList(SharingListQuery query) {
        Page<FileShareResponse> page = fileShareDao.selectFileShareListByReceiver(
                new Page<>(query.getPageNo(), query.getPageSize()),
                SessionContext.session().getIntegerId(), query.getCreatedDate()
        );
        page.getRecords().forEach(it -> {
            @SuppressWarnings(value = "all")
            final String fileExt = Files.getFileExtension(it.getFileName());
            it.setStatus(getStatus(it.getShareExpired())).setFileExt(fileExt).setFileType(FileTypeUtil.getType(fileExt));
            if (StringUtils.equals(it.getShareStatus(), DELETED)) {
                it.setStatus(DISABLED);
            }
            it.setEncrypted(StringUtils.isNotBlank(it.getSharePwd()));
        });
        return page;
    }

    @Override
    public boolean shareCancel(SharingCancelQuery query) {
        return CollectionUtils.size(query.getShareIds()) == fileShareDao.updateShareStatus(query.getShareIds());
    }

    @Override
    public boolean checkPwd(SharingCheckPwdQuery query) {
        FileShareEntity entity = fileShareDao.selectByIdAndPwd(query);
        return entity != null;
    }

    public String getStatus(String date) {
        if (DateUtil.afterEq(date, DateUtil.nowDate())) {
            return ENABLED;
        }
        return DISABLED;
    }
}
